package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.City;
import net.sf.json.JSONArray;
import service.SearchCityService;

/**
 *查找市
 */
public class SearchCityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 通过pid查找city
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码
		response.setContentType("text/html;charset=utf-8");
		//获取pid
		String pid=request.getParameter("pid");
		
		//调用service方法，返回list集合
		List<City> list=null;
		try {
			list = new SearchCityService().findcities(pid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//写回去，json
		if(list!=null && list.size()>0){
			response.getWriter().println(JSONArray.fromObject(list));
			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
